package com.cl.code.database.mybatis.module.sql.table;

import java.lang.reflect.Field;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class LogicDeleteField extends TableField {

    private static final Class<?>[] SUPPORTED_TYPES = {Long.class, Integer.class};

    public LogicDeleteField(String fieldName, String sqlFieldName, Field field) {
        super(fieldName, sqlFieldName, field);

        // 分析逻辑删除字段的类型是否被支持
        Class<?> type = field.getType();
        boolean supported = false;
        for (Class<?> supportedType : SUPPORTED_TYPES) {
            if (supportedType.isAssignableFrom(type)) {
                supported = true;
                break;
            }
        }
        if (!supported) {
            throw new IllegalArgumentException("unsupported logic delete field type: " + type);
        }
    }

    public Class<?> getType() {
        return field.getType();
    }

    public Object getNotDeleteTag() {
        if (Long.class.isAssignableFrom(getType())) {
            return 0L;
        } else if (Integer.class.isAssignableFrom(getType())) {
            return 0;
        }
        throw new IllegalArgumentException("unsupported type: " + getType());
    }

    public Object getDeletedTag() {
        if (Long.class.isAssignableFrom(getType())) {
            return 1L;
        } else if (Integer.class.isAssignableFrom(getType())) {
            return 1;
        }
        return null;
    }

}
